package com.cy.store.controller;

import com.cy.store.entity._Address;
import com.cy.store.service.IAddressService;
import com.cy.store.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("addresses")
public class AddressController extends BaseController{
    @Autowired
    private IAddressService addressService;

    @RequestMapping("add_new_address")
    public JsonResult<Void> addNewAddress(_Address address, HttpSession session){
        System.out.println("准备进入控制层");
        System.out.println(getUidFromSession(session));
        System.out.println(getUsernameFromSession(session));
        //System.out.println(address.getAddress().getClass().getName());
        System.out.println(address);
        addressService.addAddress(getUidFromSession(session), getUsernameFromSession(session), address);
        return new JsonResult<>(OK);
    }

    @GetMapping({"", "/"})
    public JsonResult<List<_Address>> getByUid(HttpSession session) {
        List<_Address> data = addressService.getByUid(getUidFromSession(session));
        return new JsonResult<>(OK, data);
    }

    @RequestMapping("{aid}/set_default")
    public JsonResult<Void> setDefault(@PathVariable("aid") Integer aid, HttpSession session) {
        addressService.setDefault(aid, getUidFromSession(session), getUsernameFromSession(session));
        return new JsonResult<Void>(OK);
    }

    @RequestMapping("{aid}/delete")
    public JsonResult<Void> delete(@PathVariable("aid") Integer aid, HttpSession session) {
        addressService.delete(aid, getUidFromSession(session), getUsernameFromSession(session));
        return new JsonResult<Void>(OK);
    }
}
